package com.hsteach.web.controller.rota;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.poi.ExcelUtil;
import com.hsteach.dao.domain.rota.RotaDetail;
import com.hsteach.dao.domain.rota.RotaPerson;
import com.hsteach.enums.ApplyStatusEnum;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.service.rota.RotaTransferService;
import com.hsteach.web.controller.buildingApply.dto.ListReviewDto;
import com.hsteach.web.controller.rota.dto.AddRotaTransferDto;
import com.hsteach.web.controller.rota.dto.ReviewRotaTransferDto;
import com.hsteach.web.dto.PageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hsteach.dao.domain.rota.RotaTransfer;

/**
 * 值班调换Controller
 * 
 * @author fanxquer
 * @date 2023-10-22
 */
@Api(value = "值班调班管理", tags = "值班调班管理")
@RestController
@RequestMapping("/rota/transfer")
public class RotaTransferController extends BaseController
{
    @Autowired
    private RotaTransferService rotaTransferService;

    /**
     * 查询我的调班申请
     */
    @ApiOperation("查询我的调班申请")
    @PostMapping("/listApply")
    public TableDataInfo listApply(@RequestBody PageDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        RotaTransfer query = new RotaTransfer();
        query.setFromPersonId(SecurityUtils.getAuthInfo().getPersonId());
        List<RotaTransfer> list = rotaTransferService.selectRotaTransferList(query);
        return getDataTable(list);
    }

    /**
     * 查询我的调班申请
     */
    @ApiOperation("查询调班审批列表")
    @PostMapping("/listReview")
    public TableDataInfo listReview(@RequestBody ListReviewDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        RotaTransfer query = new RotaTransfer();
        if (BooleanEnum.FALSE.getCode().equals(dto.getApplyStatus()) || dto.getApplyStatus() == null) {
            query.setTransferStatus(ApplyStatusEnum.APPLY.getCode());
        } else {
            query.setSearchStatus(Lists.newArrayList(ApplyStatusEnum.REJECT.getCode(), ApplyStatusEnum.PASS.getCode()));
        }
        query.setToPersonId(SecurityUtils.getAuthInfo().getPersonId());
        List<RotaTransfer> list = rotaTransferService.selectRotaTransferList(query);
        return getDataTable(list);
    }

    /**
     * 查询我的可调换日期
     */
    @ApiOperation("查询我的可调换日期")
    @GetMapping("/listFromDate/{rotaId}")
    public TableDataInfo listFromDate(@PathVariable("rotaId") Long rotaId)
    {
        List<RotaDetail> list = rotaTransferService.getMyFromDate(rotaId);
        return getDataTable(list);
    }

    /**
     * 查询可调换人员
     */
    @ApiOperation("查询可调换人员")
    @GetMapping("/listToPerson")
    public TableDataInfo listToPerson(@RequestParam("rotaId") Long rotaId, @RequestParam("transFromDate") String transFromDate)
    {
        List<RotaPerson> list = rotaTransferService.listToPerson(rotaId, transFromDate);
        return getDataTable(list);
    }

    /**
     * 查询调班后的可调换日期
     */
    @ApiOperation("查询调班后的可调换日期")
    @GetMapping("/listToDate")
    public TableDataInfo listToDate(@RequestParam("rotaId") Long rotaId, @RequestParam("toPersonId") String toPersonId)
    {
        List<RotaDetail> list = rotaTransferService.listToDate(rotaId, toPersonId);
        return getDataTable(list);
    }

    /**
     * 导出值班调换列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, RotaTransfer rotaTransfer)
    {
        List<RotaTransfer> list = rotaTransferService.selectRotaTransferList(rotaTransfer);
        ExcelUtil<RotaTransfer> util = new ExcelUtil<RotaTransfer>(RotaTransfer.class);
        util.exportExcel(response, list, "值班调换数据");
    }

    /**
     * 获取值班调换详细信息
     */
    @ApiOperation("获取调班详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rotaTransferService.selectRotaTransferById(id));
    }

    /**
     * 新增值班调换
     */
    @ApiOperation("发起调班申请")
    @PostMapping
    public AjaxResult add(@RequestBody AddRotaTransferDto dto)
    {
        return toAjax(rotaTransferService.add(dto));
    }

//    /**
//     * 新增值班调换
//     */
//    @ApiOperation("test")
//    @PostMapping("test")
//    public AjaxResult send()
//    {
//        rotaTransferService.sendMsg(SecurityUtils.getAuthInfo().getPersonId(), "刘尧老师");
//        return AjaxResult.success();
//    }

    /**
     * 审核调班申请
     */
    @ApiOperation("审核调班申请")
    @PutMapping("/review")
    public AjaxResult review(@RequestBody ReviewRotaTransferDto dto)
    {
        return toAjax(rotaTransferService.review(dto));
    }

    /**
     * 删除值班调换
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rotaTransferService.deleteRotaTransferByIds(ids));
    }
}
